package com.example.mywechatprogram.service;

import com.example.mywechatprogram.entity.Rank;
import com.example.mywechatprogram.entity.Response;
import com.example.mywechatprogram.entity.TotalRank;
import com.example.mywechatprogram.mapper.RankMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
public class RankService {
    @Autowired
    RankMapper rankMapper;
    @Autowired
    RedisTemplate redisTemplate;

    public Response add(String nickname){
        LocalDate today = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String todayString = today.format(formatter);
        try {
            int index=rankMapper.add(nickname,todayString);
            if(index==1)return new Response(Response.SERVICE_RUN_SUCCESSFULLY,null,"添加成功");
            else return new Response(Response.SERVICE_RUN_FIELD,null,"内部错误");
        }catch (DuplicateKeyException e){
            return new Response(Response.SERVICE_RUN_FIELD,null,"今日已添加");
        }catch (Exception e){
            return new Response(Response.SERVICE_RUN_FIELD,null,"内部错误2");
        }
    }
    public Response update(String nickname){
        try {
            LocalDate today = LocalDate.now();
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            String todayString = today.format(formatter);

            int temp = rankMapper.updateTotalRank(nickname,todayString);
            if (temp==0){
                int i = rankMapper.insertTotalRank(nickname,todayString);
            }
            int index=rankMapper.update(nickname,todayString);
            if(index==1)return new Response(Response.SERVICE_RUN_SUCCESSFULLY,null,"更新成功");
            else return new Response(Response.SERVICE_RUN_FIELD,null,"内部错误");
        }catch (Exception e){
            return new Response(Response.SERVICE_RUN_FIELD,null,"内部错误");
        }
    }

    public Response queryToday(String nickname){
        LocalDate today = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String todayString = today.format(formatter);
        String key = "nickname_date"+nickname+todayString;
        try {
            Object o = redisTemplate.opsForValue().get(key);
            System.out.println(o);
            if(o!=null)
            {
                System.out.println("查询缓存");

                return new Response(Response.SERVICE_RUN_SUCCESSFULLY,o,"查询成功");
            }
            else {
                Integer index=rankMapper.queryToday(nickname,todayString);
                if(index!=null){
                    System.out.println("查询数据库");
                    long ttlInSeconds = 15;
                    redisTemplate.opsForValue().set(key,index);
                    redisTemplate.expire(key, ttlInSeconds, TimeUnit.SECONDS);
                    return new Response(Response.SERVICE_RUN_SUCCESSFULLY,index,"查询成功");
                }
                else {
                    long ttlInSeconds = 15;
                    redisTemplate.opsForValue().set(key,index);
                    redisTemplate.expire(key, ttlInSeconds, TimeUnit.SECONDS);
                    return new Response(Response.SERVICE_RUN_SUCCESSFULLY,null,"今日无记录");
                }
            }
        }catch (Exception e){
            System.out.println(e.getMessage());
            return new Response(Response.SERVICE_RUN_FIELD,null,"内部错误");
        }
    }
    public Response query(int day){
        if(day>=50)return new Response(Response.SERVICE_RUN_FIELD,null,"查询的日期过于久远");
        try {
            LocalDate today = LocalDate.now();
            today = today.minusDays(day);
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            String todayString = today.format(formatter);
            String key = todayString;
            Object o = redisTemplate.opsForValue().get(todayString);
            if(o==null){
                List<Rank> list= rankMapper.query(todayString);
                long ttlInSeconds = 15;
                redisTemplate.opsForValue().set(key,list);
                redisTemplate.expire(key, ttlInSeconds, TimeUnit.SECONDS);
                if(list!=null){
                    return new Response(Response.SERVICE_RUN_SUCCESSFULLY,list,"查询成功");
                }
                else return new Response(Response.SERVICE_RUN_FIELD,null,"榜单中没有用户");
            }
            else {
                return new Response(Response.SERVICE_RUN_SUCCESSFULLY,o,"查询成功");
            }
        }catch (Exception e){
            return new Response(Response.SERVICE_RUN_FIELD,null,"内部错误");
        }
    }
    public Response queryTotalRank(){
        try {
            List<TotalRank> list= rankMapper.queryTotalRank();
            if(!list.isEmpty())return new Response(Response.SERVICE_RUN_SUCCESSFULLY,list,"查询成功");
            else return new Response(Response.SERVICE_RUN_SUCCESSFULLY,null,"查询失败");
        }catch (Exception e){
            return new Response(Response.SERVICE_RUN_FIELD,null,"内部错误");
        }

    }
}
